class SlaInstance
  def initialize(cf_client)
    @cf_client = cf_client
  end

  def find_all_sla_instances()
    @cf_client.list_sla_instances || []
  end



  def create(name, instances, memsize,disksize, framework, runtime, service,app_starttime,app_stoptime,price,fine,pricing_mode)
    raise I18n.t('sla_instances.model.name_blank') if name.blank?                  #判定是否为空
    raise I18n.t('sla_instances.model.name_invalid', :name => name) if (name =~ /^[\w-]+$/).nil?           #匹配不在中括号内的 [0-9A-Za-z_ -]
    begin
      app_info = @cf_client.app_info(name)
    rescue
      app_info = nil
    end
    raise I18n.t('sla_instances.model.already_exists') unless app_info.nil?
    raise I18n.t('sla_instances.model.instances_blank') if instances.blank?
    raise I18n.t('sla_instances.model.instances_numeric') if (instances =~ /^\d+$/).nil?
    raise I18n.t('sla_instances.model.instances_lt1') if instances.to_i < 1
    raise I18n.t('sla_instances.model.memsize_blank') if memsize.blank?
    raise I18n.t('sla_instances.model.memsize_blank') if disksize.blank?
    raise I18n.t('sla_instances.model.memsize_numeric') if (memsize =~ /^\d+$/).nil?
    raise I18n.t('sla_instances.model.memsize_unavailable') unless check_has_capacity_for(instances.to_i * memsize.to_i)
    raise I18n.t('sla_instances.model.framework_blank') if framework.blank?
    raise I18n.t('sla_instances.model.runtime_blank') if runtime.blank?
    raise I18n.t('sla_instances.model.app_starttime_blank') if app_starttime.blank?
    raise I18n.t('sla_instances.model.app_stoptime_blank') if app_stoptime.blank?
                                                                                   #raise I18n.t('sla_instances.model.app_starttime_invalid') if (app_starttime=/^(#{/[1-9]\d\d\d/}\.#{/0?[1-9]|1[12]/}\.#{/0?[1-9]|[12]\d|3[01]/}) (#{/[01]?[1-9]|2[0-4]/}:#{/[0-5]\d/}:#{/[0-5]\d/})$/).nil?
                                                                                   #raise I18n.t('sla_instances.model.app_stoptime_invalid') if(app_stoptime=/^(#{/[1-9]\d\d\d/}\.#{/0?[1-9]|1[12]/}\.#{/0?[1-9]|[12]\d|3[01]/}) (#{/[01]?[1-9]|2[0-4]/}:#{/[0-5]\d/}:#{/[0-5]\d/})$/).nil?
    raise I18n.t('sla_instances.model.framework_invalid') unless valid_framework_and_runtime?(framework, runtime)
    manifest = {                                                     #配置文件
                                                                     :name => name,
                                                                     :instances => instances,
                                                                     :resources => {:memory => memsize,:disk=>disksize},
                                                                     :staging => {:framework => framework, :runtime => runtime},
    }

    manifest[:services] = service unless service.blank?
    manifest[:app_starttime] = app_starttime unless app_starttime.blank?
    manifest[:app_stoptime] = app_stoptime unless app_stoptime.blank?
    manifest[:pricing_mode] = pricing_mode unless pricing_mode.blank?
    manifest[:price] = price unless price.blank?
    manifest[:fine] = fine unless fine.blank?
    puts "manifest------------------------------#{manifest}-----------------------------------------------"

    @cf_client.create_sla_instance(name, manifest)

  end


  def check_has_capacity_for(wanted_mem)
    system = System.new(@cf_client)
    available_for_use = system.find_available_memory()
    (available_for_use - wanted_mem.to_i) >= 0
  end


  def valid_framework_and_runtime?(framework, runtime)
    system = System.new(@cf_client)
    frameworks = system.find_all_frameworks()
    frameworks.each do |fwk_name, fwk|
      fwk[:runtimes].each do |run|
        return true if (fwk[:name] == framework && run[:name] == runtime)
      end
    end
    false
  end



  def delete(name)
    raise I18n.t('sla_instances.model.name_blank') if name.blank?
    @cf_client.delete_sla_instance(name)
  end

  def find(name)
    raise I18n.t('sla_instances.model.name_blank') if name.blank?
    puts"find  sla_instance_info  name=#{name}"
    sla_instance_info = @cf_client.sla_instance_info(name) || {}

    puts"find  sla_instance_info =#{sla_instance_info}"
    if !sla_instance_info.nil?
      sla_instance_info = nil
      sla_instances = find_all_sla_instances()
      sla_instances.each do |sla_instance_item|
        if sla_instance_item[:name] == name
          sla_instance_info = sla_instance_item
          break
        end
      end
    end
    sla_instance_info
  end














  #def find(name)
  #  raise I18n.t('apps.model.name_blank') if name.blank?
  #  app_info = @cf_client.app_info(name) || {}
  #  unless app_info.empty?
  #    app_info[:instances_info] = find_app_instances(name)
  #    app_info[:crashes] = find_app_crashes(name)
  #    app_info[:instances_states] = find_app_instances_states(app_info)
  #    app_info[:env].collect! { |env|
  #      var, value = env.split("=")
  #      {:var_name => var, :var_value => value}
  #    }
  #  end
  #  app_info
  #end
  #
  #

  #
  #
  #
  #def delete(name)
  #  raise I18n.t('apps.model.name_blank') if name.blank?
  #  @cf_client.delete_app(name)
  #end




end